<?php
/**
 *  后台菜单权限管理
*/

namespace app\admin\service;
use app\admin\model\RoleAuth;
use app\facade\Basic;
use app\admin\model\Menu as ModelMenu;
use think\facade\Db;

class MenuRole extends BaseService
{
    private $status = ['禁用','启用'];

    /**
     *  菜单管理列表
    */
    public function list($param)
    {
        $where[] = ['parent_id','=',$param['id']];
        $where[] = ['type','=',3];
        $where[] = ['is_del','=',0];

        $result = ModelMenu::field('id,btnkey,name,method,route,sort')->where($where)->select();
        return Basic::data(0,'ok',$result);
    }


    /**
     *  创建菜单
    */
    public function save($param)
    {
        $insData = [];
        $insData['parent_id'] = $param['parent_id'];
        $insData['type'] = '3';
        $insData['btnkey'] = $param['btnkey'];
        $insData['name'] = $param['name'];
        $insData['method'] = $param['method'];
        $insData['route'] = $param['route'];
        $insData['sort'] = $param['sort'];

        /* 条件 */
        $where[] = ['parent_id','=',$param['parent_id']];
        $where[] = ['name','=',$param['name']];

        if(isset($param['id'])) {
            /* 修改 */
            $result = ModelMenu::where('id',$param['id'])->find();

            if(!$result){
                return Basic::data(1, '菜单不存在');
            }

            $insData['id'] = $param['id'];
            $insData['update_time'] = date("Y-m-d H:i:s",time());
            $insData['updater'] = $param['creator'];

            $result = ModelMenu::update($insData);

            if ( $result ) {
                $this->logger->logOperation($result->id,'MenuRole','更新','更新菜单');
                return Basic::data(0, '更新成功');
            } else {
                return Basic::data(1, '更新失败');
            }

        } else {

            /* 创建 */
            $result = ModelMenu::where($where)->find();

            if($result){
                return Basic::data(1, '菜单已存在');
            }

            $insData['create_time'] = date("Y-m-d H:i:s",time());
            $insData['creator'] = $param['creator'];
            $insData['update_time'] = null;
            $insData['updater'] = null;
            $insData['is_del'] = 0;

            $result = ModelMenu::create($insData);

            if ( $result ) {
                $this->logger->logOperation($result->id,'MenuRole','创建','创建菜单');
                return Basic::data(0, '创建成功');
            } else {
                return Basic::data(1, '创建失败');
            }

        }
    }



    /**
     *  删除
    */
    public function delete($param)
    {

        $result = ModelMenu::where('id',$param['id'])->find();

        if($result->is_del > 0) {
            return Basic::data(1, '权限按钮已删除');
        }

        if ( !$result ) {
            return Basic::data(1, '菜单不存在');
        }

        Db::startTrans();
        try {

            $result->is_del = time();
            $result->update_time = date("Y-m-d H:i:s",time());
            $result->updater = $param['creator'];
            $result->save();

            RoleAuth::where('menuid',$param['id'])->delete();

            Db::commit();

        } catch (\Exception $e) {

            Db::rollback();
            return Basic::data(1,'删除失败');
        }
        $this->logger->logOperation($result->id,'MenuRole','删除','删除菜单');
        return Basic::data(0, '删除成功');

    }
}
